package com.itwc.websocketdemo.mapper;

import com.itwc.websocketdemo.entity.AiPost;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 推文表 数据层
 */
@Mapper
public interface AiPostMapper {
    
    /**
     * 插入推文
     */
    @Insert("INSERT INTO ai_post (title, content, summary, cover_image, category, author_id, author_name, " +
            "view_count, like_count, comment_count, is_featured, is_top, sort_order, deleted, status, " +
            "publish_time, create_time, update_time) " +
            "VALUES (#{title}, #{content}, #{summary}, #{coverImage}, #{category}, #{authorId}, #{authorName}, " +
            "#{viewCount}, #{likeCount}, #{commentCount}, #{isFeatured}, #{isTop}, #{sortOrder}, #{deleted}, #{status}, " +
            "#{publishTime}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertPost(AiPost post);
    
    /**
     * 根据ID查询推文
     */
    @Select("SELECT * FROM ai_post WHERE id = #{id} AND deleted = 0")
    AiPost selectPostById(@Param("id") Long id);
    
    /**
     * 查询所有启用的推文
     */
    @Select("SELECT * FROM ai_post WHERE status = 1 AND deleted = 0 ORDER BY is_top DESC, sort_order DESC, create_time DESC")
    List<AiPost> selectAllActivePosts();
    
    /**
     * 更新推文浏览量
     */
    @Update("UPDATE ai_post SET view_count = view_count + 1 WHERE id = #{id}")
    int updateViewCount(@Param("id") Long id);
}